package edu.pitt.dbmi.odie.gapp.gwt.client.analysis;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.data.DSRequest;
import com.smartgwt.client.types.VisibilityMode;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.SectionStack;
import com.smartgwt.client.widgets.layout.SectionStackSection;

import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.creation.ODIE_AnalysisCreationPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.analysis.management.ODIE_AnalysisManagementPanel;
import edu.pitt.dbmi.odie.gapp.gwt.client.dao.ODIE_DaoAnalysis;

public class ODIE_AnalysisPanel extends HLayout {
	
	/**
	 * Create a remote service proxy to talk to the server-side ODIE_ObaService
	 * service.
	 */
	private final ODIE_AnalysisServiceAsync analysisService = GWT
			.create(ODIE_AnalysisService.class);
	
	private SectionStack analysiSectionStack = null ;
	
	private SectionStackSection analysisCreationSection = null ;
	private SectionStackSection analysisManagementSection = null ;
	
	private ODIE_AnalysisCreationPanel analysisCreationPanel = null ;
	private ODIE_AnalysisManagementPanel analysisManagementPanel = null ;

	public ODIE_AnalysisPanel() {

		analysiSectionStack = new SectionStack();
		analysiSectionStack.setVisibilityMode(VisibilityMode.MUTEX);
		analysiSectionStack.setWidth("90%");
		analysiSectionStack.setHeight("90%");
		analysiSectionStack.setBorder("2px solid #458B00");

		analysisCreationPanel = new ODIE_AnalysisCreationPanel() ;
		analysisCreationPanel.setAnalysisPanel(this) ;
		analysisManagementPanel = new ODIE_AnalysisManagementPanel() ;
		analysisManagementPanel.setAnalysisPanel(this) ;
		
		analysisCreationSection = new SectionStackSection("New Analysis");
		analysisCreationSection.setID("1") ;
		analysisCreationSection.setExpanded(true);
		analysisCreationSection.setCanCollapse(true);
		analysisCreationSection.addItem(analysisCreationPanel);
		analysiSectionStack.addSection(analysisCreationSection);

		analysisManagementSection = new SectionStackSection("Manage Analyses");
		analysisManagementSection.setID("2") ;
		analysisManagementSection.setExpanded(false);
		analysisManagementSection.setCanCollapse(true);
		analysisManagementSection.addItem(analysisManagementPanel);
		analysiSectionStack.addSection(analysisManagementSection);
			
		setMembersMargin(20);
		addMember(analysiSectionStack);

		draw();
	}
	
	private void xFerToStaging(ODIE_DaoAnalysis result) {
		this.analysiSectionStack.expandSection("2") ;
		DSRequest requestProperties = new DSRequest() ;
		requestProperties.setShowPrompt(false) ;
		this.analysisManagementPanel.getListGrid().fetchData(null, null, requestProperties) ;
		this.analysisManagementPanel.getListGrid().redraw() ;
		if (!result.getAnalysisStatus().equals("IDLING")) {
			stageAnalysis(result) ;
		}
	}
	
	private void xFerToCoding(ODIE_DaoAnalysis result) {
		this.analysiSectionStack.expandSection("2") ;
		DSRequest requestProperties = new DSRequest() ;
		requestProperties.setShowPrompt(false) ;
		this.analysisManagementPanel.getListGrid().fetchData(null, null, requestProperties) ;
		this.analysisManagementPanel.getListGrid().redraw() ;
		if (!result.getAnalysisStatus().equals("IDLING")) {
			codeAnalysis(result) ;
		}
	}
	
	public void createAnalysis(String directoryHandle, String title) {
		analysisService.newInstance(directoryHandle, title,
				new AsyncCallback<ODIE_DaoAnalysis>() {
					public void onFailure(Throwable error) {
						SC.say("Creation failed.");
					}
					public void onSuccess(ODIE_DaoAnalysis result) {
						xFerToStaging(result) ;
					}
				});
	}
	
	public void stageAnalysis(ODIE_DaoAnalysis odieAnalysis) {
		analysisService.process(odieAnalysis,
				new AsyncCallback<ODIE_DaoAnalysis>() {
					public void onFailure(Throwable error) {
						SC.say("Staging failed.");
					}
					public void onSuccess(ODIE_DaoAnalysis result) {
						xFerToCoding(result) ;
					}
				});
	}
	
	public void codeAnalysis(ODIE_DaoAnalysis odieAnalysis) {
		analysisService.process(odieAnalysis,
				new AsyncCallback<ODIE_DaoAnalysis>() {
					public void onFailure(Throwable error) {
						SC.say("Coding failed.");
					}
					public void onSuccess(ODIE_DaoAnalysis result) {
						xFerToCoding(result) ;
					}
				});
	}

	public void processLogin() {
		this.analysisCreationPanel.processLogin() ;
		
	}

}
